

#include "propertyInspector.h"
#include "renderer.h"
#include "input.h"

PropertyInspector::PropertyInspector(){

    bOpen=false;
    initialLocation=location;
    tabWidth=350;
    listHeight=15;
    listWidth=150;
    level=0;
    textureID="icon_advanced";
    oldParent=NULL;

    maxListItems=(int) ((renderer->screenY* 2.0/3.0) / listHeight );
}

PropertyInspector::~PropertyInspector(){}


void PropertyInspector::setup(){

    Button::setup();
}

void PropertyInspector::update(double deltaTime){

        if (input->selectedActors.size()>0){
            parent=input->selectedActors[0];
            if ((parent!=oldParent && bOpen) || (listButton.size()==0 && parent)){
                for (uint i=0;i<listButton.size();i++){
                    listButton[i]->remove();
                }
                deselect(level);
                assembleList();
                oldParent=parent;
                cout << "recompiling list..." << endl;
                }
         }
}


void PropertyInspector::drawPlane(){

    Button::drawPlane();

    if (bOpen){
        renderer->setupShading("color");
        renderer->drawPlane(scale.x ,0.0, scale.x+tabWidth,renderer->screenY, Vector4f(0.4,0.4,0.4, 1.0));
    }

}

void PropertyInspector::mouseOver(){

    Button::mouseOver();
}

void PropertyInspector::mouseDrag(){}

//implement moving left and revealing a bigger space...
void PropertyInspector::clickedLeft(){


    if (!bOpen){
        initialLocation=location;
        location.x-=tabWidth;
        bOpen=true;
        oldParent=parent;                           //what does this mean? - it is there so we can check if we clicked on a new actor to recompile the list
        ListButton::clickedLeft();
    }
    else{
        for (uint i=0;i<listButton.size();i++){
            listButton[i]->remove();
         }
        if (scrollBar)
            scrollBar->remove();
        scrollBar=NULL;
        location=initialLocation;
        bOpen=false;
    }
}


void PropertyInspector::assembleList(){


    if (input->selectedActors.size()>0){
        cout << "assembling property list..." << endl;
        std::map <std::string, memberID>::iterator it;
        int i=0;
        cout << "selected actor name: " << input->selectedActors[0]->name << endl;
        for ( it=input->selectedActors[0]->property.begin() ; it != input->selectedActors[0]->property.end(); it++ ){

            //find Actor* properties
            memberID * mID=&it->second;

            const std::type_info* mType;
            mType=&(typeid(Actor*));

            if (mID->memberType->name()==mType->name()){
                renderer->actorInfo["15PickWorldButton"].actorReference->create();
                listButton.push_back(renderer->buttonList.back());
                }
            else{
                renderer->actorInfo["15TextInputButton"].actorReference->create();
                listButton.push_back(renderer->buttonList.back());
                }

            listButton[i]->name=it->first;
            listButton[i]->buttonProperty=it->first;
            listButton[i]->textureID="icon_base";
            listButton[i]->parent=input->selectedActors[0];
            listButton[i]->level=level+1;
            listButton[i]->bDrawName=true;
            listButton[i]->color=Vector4f(0.2,0.2,0.2,1.0);
            listButton[i]->sceneShaderID="color";
            if (!bOpen)
                listButton[i]->bHidden=true;
            placeButton(i,i);
            listButton[i]->setup();
            i++;
        }

    }
    //cout << "our Button list is: "<<listButton.size()<<" elements long..." << endl;
    if (bOpen)
        createScrollBar();
}

void PropertyInspector::placeScrollBar(){

scrollBar->location.x=location.x+10;
scrollBar->location.y=location.y+scale.y +2;
}

void PropertyInspector::clickedRight(){}

void PropertyInspector::focusClick(){

    Button::focusClick();
}

void PropertyInspector::deselect(int depth){

    ListButton::deselect(depth);
}

void PropertyInspector::create(){renderer->addButton(this);}
